﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Windows.Forms;
using System.Security.Cryptography;
using GUIServerConnectorAPI;
using GUI;

namespace LoginForm
{
    public partial class LoginForm : Form
    {
        private PacientForm pacientForm;
        private IngrijitorForm ingrijitorForm;
        private SupraveghetorForm supraveghetorForm;
        private MedicForm medicForm;
        private AdminForm adminForm;

        public LoginForm()
        {
            InitializeComponent();
            pacientForm = new PacientForm(this);
            ingrijitorForm = new IngrijitorForm(this);
            supraveghetorForm = new SupraveghetorForm(this);
            medicForm = new MedicForm(this);
            adminForm = new AdminForm(this);
        }

        public void Reset(Form form)
        {
            if (form == pacientForm)
                pacientForm = new PacientForm(this);
            else if (form == ingrijitorForm)
                ingrijitorForm = new IngrijitorForm(this);
            else if (form == supraveghetorForm)
                supraveghetorForm = new SupraveghetorForm(this);
            else if (form == medicForm)
                medicForm = new MedicForm(this);
            else if (form == adminForm)
                adminForm = new AdminForm(this);
            else
                throw new InvalidOperationException(Strings.IllegalResetRequestor);
            TXTUsername.Text = "";
            TXTPassword.Text = "";
            this.Show();
        }

        private void BTNOK_Click(object sender, EventArgs e)
        {
         //   SHA1 shpass=new SHA1CryptoServiceProvider();
         //   shpass.Hash = textBox2.Text;
       //     String pass = shpass.ComputeHash(textBox2.Text);
            MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
            byte[] passwordHash = md5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(TXTPassword.Text));

            try
            {
                LoginData loginData = GUIServerConnector.getInstance().getAuthenticator().login(TXTUsername.Text, passwordHash);

                this.Hide();
                switch (loginData.accountType)
                {
                    case AccountType.PACIENT:
                        pacientForm.Show(loginData.sessionId);
                        break;
                    case AccountType.INGRIJITOR:
                        ingrijitorForm.Show(loginData.sessionId);
                        break;
                    case AccountType.SUPRAVEGHETOR:
                        supraveghetorForm.Show(loginData.sessionId);
                        break;
                    case AccountType.MEDIC:
                        medicForm.Show(loginData.sessionId);
                        break;
                    case AccountType.MANAGER:
                        adminForm.Show(loginData.sessionId);
                        break;
                    default:
                        throw new InvalidOperationException(Strings.InvalidAccountType);
                }
            }
            catch (AuthenticationException ex)
            {
                TXTUsername.Text = "";
                TXTPassword.Text = "";
                MessageBox.Show(this, ex.Message, "AuthenticationException", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }

            /*
            if (TXTUsername.Text == "1" && TXTPassword.Text == "2")
            {
                this.Hide();
                PacientForm Pacfm = new PacientForm();
                Pacfm.Show();
            }
            else
                if (TXTUsername.Text == "3" && TXTPassword.Text == "4")
                {
                    SupraveghetorForm Suprfm = new SupraveghetorForm();
                    Suprfm.Show();
                    this.Hide();
                }
                else
                    if (TXTUsername.Text == "5" && TXTPassword.Text == "6")
                    {
                        MedicForm Medfm = new MedicForm();
                        Medfm.Show();
                        this.Hide();
                    }
                    else
                        if (TXTUsername.Text == "7" && TXTPassword.Text == "8")
                        {
                            IngrijitorForm Ingfm = new IngrijitorForm();
                            Ingfm.Show();
                            this.Hide();
                        }
                        else
                            MessageBox.Show("Ati introdus user sau parola gresita");
             */
        }

        private void BTNCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
